<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Repositories\PostRepository;
use App\Repositories\CategoryRepository;

class HomeController extends Controller
{
    // The PostRepository instance
    protected $post;
    // The CategoryRepository instance
    protected $category;
    /**
     * Create a new controller instance.
     * HomeController constructor.
     * @param PostRepository $post
     * @param CategoryRepository $category
     */
    public function __construct(PostRepository $post,CategoryRepository $category)
    {
        $this->post = $post;
        $this->category = $category;
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $postList = $this->post->getPosts();
        return view('back.home',compact('postList'));
    }

    /**
     * Show the application edit page
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id){
        $postDetail = $this->post->getDetail($id);
        $category = $this->category->getAllCategory();
        return view('back.edit',compact('postDetail','id','category'));
    }

    /**
     * Show the add post page
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add() {
        return view('back.add');
    }

    /**
     * 保存或者更新 文章
     * @param Request $request
     * @return string
     */
    public function save(Request $request){
        if($request->is('post')){
            $request->all();
            return 'hello world';
        } else {
            return $request->all();
        }
    }
}
